//
//  HELAppManager.m
//  MobileClinicalOnline
//
//  Created by wsj2015 on 16/7/13.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELAppManager.h"
static HELAppManager *appManager;
@implementation HELAppManager


+ (instancetype)sharedAppManger{
    if (!appManager) {
        appManager = [[HELAppManager alloc] init];
    }
    return appManager;
}


+ (BTUserInfoM *)userInfo {
    NSDictionary *userInfo = [[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
    BTUserInfoM *userModel = [BTUserInfoM mj_objectWithKeyValues:userInfo];
    return userModel;
}

//退出登录
+ (void)loginOut {

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults removeObjectForKey:@"userInfo"];
    [userDefaults removeObjectForKey:COOKIE_KEY];
    [userDefaults removeObjectForKey:@"cookieDate"];
    [userDefaults synchronize];
}


//保存用户信息
+(void)saveUserInfoAndCookie:(NSDictionary *)infos {
    [self loginOut];
    //存储用户信息 和 sessionid
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    BTUserInfoM *userM = [BTUserInfoM mj_objectWithKeyValues:infos[@"user"]];
    NSDictionary *userInfos = [userM mj_keyValues];
    [userDefaults setObject:userInfos forKey:@"userInfo"];
    [self saveCookie:infos[@"xauthToken"]];
    
    if (![userDefaults objectForKey:@"login"]) {
        [userDefaults setBool:YES forKey:@"login"];
    }
    [userDefaults synchronize];
}

+ (void)saveUserCampId:(NSInteger)campId{
  NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  [userDefaults setObject:@(campId) forKey:@"CAMPID"];
  [userDefaults synchronize];
}
+ (NSInteger)getUserCampId{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSNumber *campId = [userDefaults objectForKey:@"CAMPID"];
    if (!campId) {
        return -1;
    }else{
        return [campId integerValue];
    }
}

//更新密码
+(void)updatePassword:(NSString *)password {

    BTUserInfoM *userM = [self userInfo];
    userM.password = password;
    NSDictionary *userInfos = [userM mj_keyValues];
    [[NSUserDefaults standardUserDefaults] setValue:userInfos forKey:@"userInfo"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

//保存sessionid
+(void)saveCookie:(NSString *)sessionId {
    
    [[NSUserDefaults standardUserDefaults] setObject:sessionId forKey:COOKIE_KEY];
    //cookie的起始时间
    NSDate *storeDate = [NSDate date];
    [[NSUserDefaults standardUserDefaults] setObject:storeDate forKey:@"cookieDate"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}



+ (void)jSessionIdOverTime:(void (^)(NSString *sessionID))session sessionFailure:(void (^)())sessionFailure {
    
    NSString *jsessionId = [[NSUserDefaults standardUserDefaults] objectForKey:COOKIE_KEY];
    session(jsessionId);
    return;
    
    
    //以下是为静默登录而做
    //没有超时，直接返回sessionid
    NSDate *oldDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"cookieDate"];
    NSDate *nowDate = [NSDate date];
    BOOL isOverTime = [nowDate timeIntervalSinceDate:oldDate] - 1500 > 0 ? YES:NO;
    
    
    if (!isOverTime) {
        //没有超时，直接返回sessionid
        NSString *jsessionId = [[NSUserDefaults standardUserDefaults] objectForKey:COOKIE_KEY];
        session(jsessionId);
        
    }else {
        //超时，1.置空session  2.去重新登录请求新的session
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:COOKIE_KEY];
        NSDictionary *param = @{@"email":[[self userInfo] busnEmailAddr], @"password":[[self userInfo] password]};
        [[BTNetworkManager sharedMg] NoSession_POST:BTLoginUrl parameters:param success:^(id responseObject) {
            if ([responseObject[@"success"] boolValue]) {
                [HELAppManager saveUserInfoAndCookie:responseObject[@"data"]];
                session(responseObject[@"data"][@"xauthToken"]);
            }else {
                
                sessionFailure();
                [[NSNotificationCenter defaultCenter] postNotificationName:FailureToGetSessionIDNotification object:nil];
            }
            
        } failure:^(NSString *err) {
            alertMessage(err);
            sessionFailure();
        }];
    }
}





@end
